package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.entity.codetb;
import com.wanjia.seal.manager.mapper.CodeTBMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
@Api(tags = "代码信息子表管理")
public class CodeTypeController  extends BaseController{

    @Autowired
    CodeTBMapper codeTBMapper;


    @ResponseBody
    @PostMapping("/codetype")
    @ApiOperation(value = "添加子表类型", notes = "添加是否成功")
    @LoginToken
    public JsonResult createByCodeType(@RequestBody codetb codetb){
        Integer NewCodeID = codetb.getCodeID();
        String Reserved = codetb.getReserved();
        Integer CodeID = codetb.getCodeType();
        String CodeName = codetb.getCodeName();
        codetb result = codeTBMapper.findByCodeTypeID(CodeID, NewCodeID);
        if (result!=null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        codeTBMapper.createByCodeType(CodeID, NewCodeID, CodeName, Reserved);
        return new JsonResult(OK);
    }

    @ResponseBody
    @GetMapping("/codetype/id")
    @ApiOperation(value = "ID查询子表类型", notes = "ID查询是否成功")
    @LoginToken
    public JsonResult<codetb> selectByCodeType(Integer CodeID,Integer SonCodeID){
        codetb result = codeTBMapper.findByCodeTypeID(CodeID, SonCodeID);
      /*  if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }*/
        return new JsonResult<>(OK,result);
    }

    @ResponseBody
    @GetMapping("/codestype")
    @ApiOperation(value = "查询所有子表类型", notes = "查询是否成功")
    @LoginToken
    public JsonResult<Object> selectsByCodeType(Integer CodeID,Integer pageNum, Integer pageSize){
        List<codetb> byCodeType = codeTBMapper.findByCodeType(CodeID,pageNum,pageSize);
        /*if (byCodeType.isEmpty()){
            throw new DataNotFoundException("数据不存在的异常");
        }*/
        for (codetb codetb : byCodeType) {
            System.out.println(codetb);
        }
        Integer rows = codeTBMapper.findByCodeTypeTOTAL(CodeID);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("codetb",byCodeType);
        return new JsonResult<>(OK,jsonObject);
    }

    @ResponseBody
    @PutMapping("/codetype/id")
    @ApiOperation(value = "更新子表类型", notes = "更新是否成功")
    @LoginToken
    public JsonResult updateByCodeType(@RequestBody codetb codetb){
        Integer codeType = codetb.getCodeType();
        Integer NewCodeID = codetb.getCodeID();
        String NewCodeName = codetb.getCodeName();
        String NewReserved = codetb.getReserved();
        codetb result = codeTBMapper.findByCodeTypeID(codeType, NewCodeID);
        if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        codeTBMapper.updateByCodeType(codeType, NewCodeID, NewCodeName, NewReserved);
        return new JsonResult(OK);
    }

    @ResponseBody
    @DeleteMapping("/codetype/id")
    @ApiOperation(value = "删除子表类型", notes = "删除是否成功")
    @LoginToken
    public JsonResult DeleteByCodeType(Integer CodeID,Integer SonCodeID){
        codetb result = codeTBMapper.findByCodeTypeID(CodeID, SonCodeID);
        if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        codeTBMapper.deleteByCodeType(CodeID,SonCodeID);
        return new JsonResult(OK);
    }
}
